package com.grandbroad.eim.wedgit;

import android.animation.ValueAnimator;
import android.app.Activity;
import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.support.annotation.LayoutRes;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.grandbroad.eim.R;
import com.grandbroad.eim.entry.VipBean;

public class VipPopupWindow extends PopupWindow implements View.OnClickListener
{
    private final TextView tv_vip_price;
    private final TextView tv_pay;
    private onBottomItemClickListener onBottomItemClickListener;

    public void setOnBottomItemClickListener(VipPopupWindow.onBottomItemClickListener onBottomItemClickListener) {
        this.onBottomItemClickListener = onBottomItemClickListener;
    }

    /**
     * 上下文
     */
    private Activity context;
    /**
     * 最上边的背景视图
     */
    private View vBgBasePicker;
    /**
     * 内容viewgroup
     */
    private LinearLayout llBaseContentPicker;
    private onBottomItemClickListener bottomItemClickListener;
    private VipBean vipBean;

    public VipPopupWindow(Activity context) {
        super(context);
        this.context = context;
        View view = View.inflate(context, R.layout.vip_pay_view, null);
         tv_vip_price = view.findViewById(R.id.tv_vip_price);
         tv_pay = view.findViewById(R.id.pay_now);
        tv_pay.setOnClickListener(this);
        tv_vip_price.setOnClickListener(this);
        setContentView(view);
        //设置PopupWindow弹出窗体的宽
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        //设置PopupWindow弹出窗体的高
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        setTouchable(true);//设置可以触摸
        setOutsideTouchable(true);//设置外边可以点击
        ColorDrawable dw = new ColorDrawable(0xffffff);
        setBackgroundDrawable(dw);
        //设置SelectPicPopupWindow弹出窗体动画效果
        this.setAnimationStyle(R.style.BottomDialogWindowAnim);




    }

    public void setBottomItemClickListener(onBottomItemClickListener bottomItemClickListener) {
        this.bottomItemClickListener = bottomItemClickListener;
    }


    /**
     * 为了适配7.0系统以上显示问题(显示在控件的底部)
     *
     * @param anchor
     */
    @Override
    public void showAsDropDown(View anchor) {
        if (Build.VERSION.SDK_INT >= 24) {
            Rect rect = new Rect();
            anchor.getGlobalVisibleRect(rect);
            int h = anchor.getResources().getDisplayMetrics().heightPixels - rect.bottom;
            setHeight(h);
        }
        super.showAsDropDown(anchor);
    }

    /**
     * 展示在屏幕的底部
     *
     * @param layoutid rootview
     */
    public void showAtLocation(@LayoutRes int layoutid) {
        showAtLocation(LayoutInflater.from(context).inflate(layoutid, null),
                Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
        /** 窗口背景变暗*/
        //dimBackground(1.0f,0.5f);
    }

    /**
     * 点击某个item
     * @param v
     */
    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.tv_vip_price:
                if (bottomItemClickListener!=null)
                {
                    bottomItemClickListener.click(v,vipBean);
                }
                break;
            case R.id.pay_now:
                if (bottomItemClickListener!=null)
                {
                    bottomItemClickListener.click(v,vipBean);
                }
                break;
        }
    }

    public void setBean(VipBean vipBean) {
        this.vipBean = vipBean;
        tv_vip_price.setText(vipBean.getNow_price()+"元");

    }

    public VipBean getVipBean() {
        return vipBean;
    }

    public interface onBottomItemClickListener
    {
        void click(View view,VipBean vipBean);
    }
    private void dimBackground(final float from, final float to) {
        final Window window = context.getWindow();
        ValueAnimator valueAnimator = ValueAnimator.ofFloat(from, to);
        valueAnimator.setDuration(500);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                WindowManager.LayoutParams params = window.getAttributes();
                params.alpha = (Float) animation.getAnimatedValue();
                window.setAttributes(params);
            }
        });

        valueAnimator.start();
    }

}
